document.write("从文件中执行javascript! <br>")

// JavaScript 有三个基本的变量类型如下
// • Numbers : 123, 32.32
// • Strings : “Meher”, “Krishna Patel”, “123”
// • Boolean : true, false


var your_name = "Meher";
var age = 20;
document.getElementById("p_name").innerHTML = "你好啊 " + your_name + "<br>年龄 : " + age;

// 字符串转换
document.write("<br> <h2>字符串转换</h2> <br> ");
document.write("2 + Number('3.4') = ", 2 + Number('3.4'), "<br>");

// 整数转换
document.write("<br> <h2>整数转换</h2> <br> ");
document.write("2 + parseInt('3.4') = ", 2 + parseInt('3.4'), "<br>"); // string to int
document.write("2 + parseInt(3.4) = ", 2 + parseInt(3.4), "<br>"); // float to int

//浮点数转换
document.write("<br> <h2>浮点数转换</h2> <br> ");
document.write("2 + parseFloat('3.4') = ", 2 + parseFloat("3.4"), "<br>");

// 字符串类 常用操作
document.write("<br> <h2>字符串类 常用操作</h2> <br> ");
document.write("meher".toUpperCase(), "<br>") // uppercase
w = "Krishna"
document.write(w.toLowerCase(), "<br>") // lowercase
document.write(w.small(), "<br>") // small
document.write(w.bold(), "<br>") // bold
document.write(w.strike(), "<br>") // strike
document.write(w.fontsize("5em"), "<br>") // strike
document.write(w.link("http://pythondsp.readthedocs.io"), "<br>") // link
document.write(w.fontcolor("red").fontsize("12em"), "<br>") // multiple

// 数学函数
document.write("<br> <h2>数学函数</h2> <br> ");
document.write("pi = ", Math.PI, "<br>");
document.write("e = ", Math.E, "<br>");
document.write("相似地，以下的函数也是可用的 'abs', 'floor', 'ceil' and 'round' etc. <br>")
document.write("随机数产生 : ", Math.ceil(Math.random() * 20), "<br>"); // enter random number


// 数组
document.write("<br> <h2>数组</h2> <br> ");
arr = [15, 30, "Meher"]
for (a in arr) {
  document.write(arr[a], " ");
}
document.write("<br>");
document.write(arr.pop(), "<br>"); // remove last element
arr.push("Krishna"); // add element to end
document.write(arr.pop(), "<br>");
document.write("lenght of array: ", arr.length, "<br>");

// 条件语句
document.write("<br> <h2>条件语句</h2> <br> ");
age = 10;
if (age > 3 && age < 6) {
  document.write("Age : " + age + "<b> go to kindergarten</b>");
} else if (age >= 6 && age < 18) {
  document.write("Age : " + age + "<b> go to school</b>");
} else {
  document.write("Age : " + age + "<b> go to college</b>");
}
document.write("<br>");

// switch语句
document.write("<br> <h2>switch语句</h2> <br> ");
var grade = 'A';
document.write("Grade " + grade + " : ");
switch (grade) {
  case 'A':
    document.write("Very good grade!");
    break;
  case 'B':
    document.write("Good grade!");
    break;
  default: // if grade is neither 'A' nor 'B'
    document.write("Enter correct grade");
}
document.write("<br>");

// for循环
document.write("<br> <h2>for循环</h2> <br> ");
for (i = 5; i >= 0; i--) {
  document.write(i + " ");
}
document.write("<br>");

// for-in 循环
document.write("<br> <h2>for-in循环</h2> <br> ");
arr = [10, 12, 31]; // array
for (a in arr) {
  document.write(arr[a] + " ");
}
document.write("<br>");

// while循环
document.write("<br> <h2>while循环</h2> <br> ");
x = 0;
while (x < 5) {
  document.write(x + " ");
  x++;
}
document.write("<br>");

// 定义函数
document.write("<br> <h2>定义函数</h2> <br> ");

function add2Num(num1, num2) { // function definition
  return num1 + num2;
}
sum = add2Num(2, 3); // function call
document.write("2 + 3 = " + sum);
document.write("<br>");

function alertMessage(message) {
  alert(message)
}

document.write("<br> <h2>输入框 整数计算</h2> <br> ");
var x = prompt("enter a number");
document.write("2 * ", x, " = ", 2 * x + "<br>");

//比较操作注意点
// • == (只比较值是否相等，不比较类型是否一致)
// • === (比较值和类型是否相同)
